package com.showT.controller;

import com.showT.domain.Users;
import com.showT.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private IUsersService usersService;

    @RequestMapping("save")
    public String save(Users users){
        usersService.save(users);

        return "redirect:/pages/login.jsp";
    }

    @RequestMapping("login")
    public ModelAndView login(Users users){
        Users user = usersService.findByUsername(users.getUsername());

        ModelAndView mv = new ModelAndView();

        if (user.getPassword().equals(users.getPassword())){
            mv.addObject("user",user);
            mv.setViewName("success");
        } else {
            mv.setViewName("login");
        }
        return mv;
    }
}
